﻿using DbEntityTools.Config;
using DbEntityTools.Service;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace DbEntityTools
{
    public partial class MainForm : Form
    {
        private MySqlConnectionService service = null;
        private List<string> dbList = new List<string>();
        private List<string> tables = new List<string>();
        public MainForm()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void InitDataBase()
        {
            this.DbCom.DataSource = dbList;
        }

        /// <summary>
        /// 数据库选择事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DbCom_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                var dbName = this.DbCom.SelectedValue.ToString();
                tables = service.GetTables(dbName);
                this.tableComb.DataSource = tables;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Show tables  Error:" + ex.Message, "提示");
            }
        }

        private void previewBut_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(DbCom.Text) == false && string.IsNullOrWhiteSpace(tableComb.Text) == false)
                {
                    var table = service.GetTableDetail(DbCom.Text.Trim(), tableComb.Text.Trim());
                    var con = DbEntityCreateService.Create(table, NameSpaceText.Text.Trim(), this.checkDataAccress.Checked);
                    contentRichText.Text = con;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void mySqlToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            try
            {
                DbLogin frm = new DbLogin();
                if (frm.ShowDialog() == DialogResult.OK)
                {
                    service = frm.service;
                    dbList.Clear();
                    dbList.AddRange(service.dbList);
                    InitDataBase();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
